#!/usr/bin/perl
use strict;

package csaccess;

sub csaccess {

    my ($dbh,$remote,$input,$csnum)=@_;
    my $sth=$dbh->prepare("SELECT num,nick,auth FROM ninfo WHERE num = '$$input[0]';");
    $sth->execute;
    my ($nick,$auth);
    while (my @tmp = $sth->fetchrow_array) {
	if ($tmp[0] eq $$input[0]) {
	    $nick = $tmp[1];
	    $auth = $tmp[2];
	}
    }
    if ($auth eq '0') {
	print $remote "$csnum O $$input[0] :You have to be identified with nickserv to use this command.\n";
    }
    else {
	if ($$input[4] eq "") {
	    print $remote "$csnum O $$input[0] :\cBAccess list for $auth\cB\n";
	    print $remote "$csnum O $$input[0] :\026Channel        LEVEL     AOP     AVOICE\n";
	    $sth=$dbh->prepare("SELECT channel,level,aop,avoice FROM users WHERE rnick = '$auth';");
	    $sth->execute;
	    while (my @tmp = $sth->fetchrow_array) {
		my $blankspace = length($tmp[0]);
		$blankspace = 15 - $blankspace;
		$blankspace = ' ' x $blankspace; 
		print $remote "$csnum O $$input[0] :$tmp[0]$blankspace$tmp[1]     $tmp[2]     $tmp[3]\n";
	    }
	}
	else {
	    $sth=$dbh->prepare("SELECT channel,level FROM users WHERE channel = '$$input[4]' and rnick = '$auth';");
	    $sth->execute;
	    my @tmp = $sth->fetchrow_array;
	    if ($tmp[1] eq "") {
		print $remote "$csnum O $$input[0] :You do not have access in $$input[4]\n";
	    }
	    else {
	         print $remote "$csnum O $$input[0] :You have $tmp[1] level access to $tmp[0]\n";
	     }
	}
    }
}
1;
